package com.lj.bruth.util;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;

    public class AlipayConfig {

        public static void main(String[] args) throws Exception {
            //调用失败，原因：Insufficient Permissions，ISV权限不足，建议在开发者中心检查应用是否上线，解决办法详见：https://docs.open.alipay.com/common/isverror
            
            // 1. 设置参数（全局只需设置一次）
            Factory.setOptions(getOptions());
            try {
                // 2. 发起API调用（以创建当面付收款二维码为例）
                AlipayTradePrecreateResponse response = Payment.FaceToFace()
                        .preCreate("Apple save money", "2234567890", "5799.00");
                // 3. 处理响应或异常
                if (ResponseChecker.success(response)) {
                    System.out.println("调用成功");
                } else {
                    System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
                }
            } catch (Exception e) {
                System.err.println("调用遭遇异常，原因：" + e.getMessage());
                throw new RuntimeException(e.getMessage(), e);
            }
        }

        private static Config getOptions() {
            Config config = new Config();
            config.protocol = "https";
            config.gatewayHost = "openapi.alipay.com";
            config.signType = "RSA2";

            config.appId = "2021002122667260";

            // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
            config.merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCp+B/6AackiCLul4U4vytZ+gYnm1vfu7zwvsbU03uYOC5uwip0uCIYuTnZ9QUT42pAxTtaYJQda5vm5Q2KcHjLkJU+I4bnNX9UQbDUKPPWAWYI2+VJvrR0HZchqAiCv5rOqHeGxEgBNiv5O+X3mq8pJxpWaF2faZCNoks8gx1ceJcx/x/+sKCjS7hETW51Og3FO/VAbh/4xzauB/usYouVa4tYUt2QUcOk5GVru0BsVE7TuKTZjvcSOgGYgfMWZ362bnOz9VA37PkpCJoZyRy/Ag2YIwsCxd6am8XqMJoGMWbFO9IDgVS2qESMUo19mMz35SRFyc1GnYB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hANZskNLx8wKcr6cjMN5e8paScPfLqus8Gwa0ElmfRf1XmkcXxIfLMR4WN3lxiYdhdYD4BPzlisahukMQwHo760VHPSlMsfvQFH1cZPD5o" +
                    "jEK8sGrLo0yYu3UBtSb7Kk2+JPaamb09dUVz+/YGJj7gG6HIbgRPSQPkRL";

            //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
            //请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt
            config.merchantCertPath = "/users/java/aliyun/appCertPublicKey_2021002122667260.crt";
            //请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt
            config.alipayCertPath = "/users/java/aliyun/alipayCertPublicKey_RSA2.crt";
            //请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt
            config.alipayRootCertPath = "/users/java/aliyun/alipayRootCert.crt";

            //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
            // config.alipayPublicKey = "<-- 请填写您的支付宝公钥，例如：MIIBIjANBg... -->";

            //可设置异步通知接收服务地址（可选）
            //config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";

            //可设置AES密钥，调用AES加解密相关接口时需要（可选）
            //config.encryptKey = "rZLbzEf3lI6HIUFn5dbR0g==";
            return config;
        }
    }